package com.probox.common.entity.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.order.OrderInfoShare;
import com.probox.common.entity.mapper.order.OrderInfoShareMapper;
import com.probox.common.entity.service.order.IOrderInfoShareService;
import com.probox.order.api.domain.ProfitShareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订单分账信息Service业务层处理
 *
 * @author usoft
 * @date 2022-01-20
 */
@Service
@Transactional
public class OrderInfoShareServiceImpl extends ServiceImpl<OrderInfoShareMapper, OrderInfoShare> implements IOrderInfoShareService {
    @Autowired
    private OrderInfoShareMapper orderInfoShareMapper;


    @Override
    public boolean save(String orderNo, String returnNo, String action, Long amount, ProfitShareVo request, ProfitShareVo response) {
        OrderInfoShare share = new OrderInfoShare();
        share.setAmount(amount);
        share.setOrderNo(orderNo);
        share.setAction(action);
        share.setReturnNo(returnNo);
        share.setCreateTime(new Date());
        if (response != null && StringUtils.isNotEmpty(response.getCode())){
            share.setResult(response.getMessage());
        }else if (response == null){
            share.setResult("返回为空");
        }else{
            share.setResult("success");
        }
        share.setRequestParameter(JSON.toJSONString(request));
        share.setRequestParameter(JSON.toJSONString(response));
        return this.save(share);
    }

    /**
     * 查询订单分账信息
     *
     * @param shareId 订单分账信息ID
     * @return 订单分账信息
     */
    @Override
    public OrderInfoShare selectOrderInfoShareById(Long shareId) {
        return this.getById(shareId);
    }

    /**
     * 查询订单分账信息列表
     *
     * @param orderInfoShare 订单分账信息
     * @return 订单分账信息
     */
    @Override
    public List<OrderInfoShare> queryList(OrderInfoShare orderInfoShare) {
        QueryWrapper<OrderInfoShare> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getShareId())) && orderInfoShare.getShareId() != null) {
            queryWrapper.eq("share_id", orderInfoShare.getShareId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getOrderNo())) && orderInfoShare.getOrderNo() != null) {
            queryWrapper.eq("order_no", orderInfoShare.getOrderNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getReturnNo())) && orderInfoShare.getReturnNo() != null) {
            queryWrapper.eq("return_no", orderInfoShare.getReturnNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getAction())) && orderInfoShare.getAction() != null) {
            queryWrapper.eq("action", orderInfoShare.getAction());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getAmount())) && orderInfoShare.getAmount() != null) {
            queryWrapper.eq("amount", orderInfoShare.getAmount());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getResult())) && orderInfoShare.getResult() != null) {
            queryWrapper.eq("result", orderInfoShare.getResult());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getRequestParameter())) && orderInfoShare.getRequestParameter() != null) {
            queryWrapper.eq("request_parameter", orderInfoShare.getRequestParameter());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInfoShare.getResponseParameter())) && orderInfoShare.getResponseParameter() != null) {
            queryWrapper.eq("response_parameter", orderInfoShare.getResponseParameter());
        }
        return orderInfoShareMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单分账信息
     *
     * @param orderInfoShare 订单分账信息
     * @return 结果
     */
    @Override
    public boolean insertOrderInfoShare(OrderInfoShare orderInfoShare) {

        return this.save(orderInfoShare);

    }

    /**
     * 修改订单分账信息
     *
     * @param orderInfoShare 订单分账信息
     * @return 结果
     */
    @Override
    public boolean updateOrderInfoShare(OrderInfoShare orderInfoShare) {

        return this.updateById(orderInfoShare);
    }

    /**
     * 批量删除订单分账信息
     *
     * @param shareIds 需要删除的订单分账信息ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderInfoShareByIds(Long[] shareIds) {
        return this.removeByIds(Arrays.asList(shareIds));
    }


}
